package cn.car.schedule;

import cn.car.domains.dto.SyncInfoDto;
import cn.car.service.config.SystemConfig;
import cn.car.service.remote.TjRemoteService;
import cn.car.service.remote.process.Processor;
import cn.car.service.remote.process.ProcessorFactory;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class TjSchedule {

    @Resource
    private SystemConfig systemConfig;

    @Resource
    private TjRemoteService tjRemoteService;

    @Resource
    private ProcessorFactory processorFactory;


    @Scheduled(fixedRate = 1,timeUnit = TimeUnit.MINUTES,initialDelay = 0)
    public void syncInfoSchedule() {

        //请求政府获取信息同步信息

        try {
            if (StrUtil.isEmpty(systemConfig.getGovEndpoint().get())){
                return;
            }

            SyncInfoDto syncInfoDto = tjRemoteService.syncInfo();

            Processor syncProcessor = processorFactory.getSupport("syncProcessor");
            syncProcessor.process(syncInfoDto);


        } catch (IOException e) {
            log.warn("[syncInfoScheduler remoteService.syncInfo] error:{}", e.getMessage(), e);
        }

    }
}
